use  "../data/zip_year_data", clear

*Define CHTV and CHTC and the IV 
gen lnchtc = log((chinese100cash - american100cash/american100*chinese100)*(american100/(american100 - american100cash)))
gen chtc = (chinese100cash - american100cash/american100*chinese100)*(american100/(american100 - american100cash))

gen lnchtv = log((chinese100_cashval - american100_cashval/american100_val*chinese100_val)*(american100_val/(american100_val - american100_cashval)))
gen chtv = (chinese100_cashval - american100_cashval/american100_val*chinese100_val)*(american100_val/(american100_val - american100_cashval))

by year, sort: egen chttc = sum(chtc)
gen chi_iv_ct = log(chineseper * chttc)
by year, sort: egen chttv = sum(chtv)
gen chi_iv_val = log(chineseper * chttv)

*Define the interacted terms
gen lnchtvXpost = lnchtv * post
gen lnchtcXpost = lnchtc * post
gen chi_iv_ctXpost = chi_iv_ct * post
gen chi_iv_valXpost = chi_iv_val * post

*Keep only common sample where both lnchtv and lnchtc are not missing
drop if lnchtc == .
drop if lnchtv == .

forvalue i= 2001/2013{
	gen lnchtc_`i' = lnchtc if year == `i'
	replace lnchtc_`i' = 0 if lnchtc_`i' ==. & lnchtc != .
	gen chi_iv_ct_`i' = chi_iv_ct if year == `i'
	replace chi_iv_ct_`i' = 0 if chi_iv_ct_`i'  == .
	}

drop lnchtc_2007 chi_iv_ct_2007

*Zillow
ivreghdfe lnzhvi_real lnpopulation d_lnzhvi_real_9600 bachelorshare d_lnincome_9801 (lnchtc lnchtc_2* = chi_iv_ct chi_iv_ct_2*) if chinese100cash!= 0, absorb(timecounty) cluster(zipcode) 

forvalue i = 2001/2006{
local beta_`i' = _b[lnchtc_`i']
local se_`i' = _se[lnchtc_`i']
}

forvalue i = 2008/2013{
local beta_`i' = _b[lnchtc_`i']
local se_`i' = _se[lnchtc_`i']
}

capture drop beta1 ul1 ll1 k ul2 ll2
gen beta1 = 0 in 7
gen ul1 = 0 in 7
gen ll1 = 0 in 7
gen ul2 = 0 in 7
gen ll2 = 0 in 7

forvalue j = 1/6{
local i = `j' + 2000
display `i' `j'
replace beta1 = `beta_`i'' in `j'
replace ul1 = `beta_`i'' + 1.96*`se_`i'' in `j'
replace ll1 = `beta_`i'' - 1.96*`se_`i'' in `j'
replace ul2 = `beta_`i'' + 1.645*`se_`i'' in `j'
replace ll2 = `beta_`i'' - 1.645*`se_`i'' in `j'
}	
 
forvalue j = 8/13{
local i = `j' + 2000
display `i' `j'
replace beta1 = `beta_`i'' in `j'
replace ul1 = `beta_`i'' + 1.96*`se_`i'' in `j'
replace ll1 = `beta_`i'' - 1.96*`se_`i'' in `j'
replace ul2 = `beta_`i'' + 1.645*`se_`i'' in `j'
replace ll2 = `beta_`i'' - 1.645*`se_`i'' in `j'
}	

gen k = 2001 in 1
replace k = k[_n-1] + 1 if k == .

preserve
keep in 1/13
twoway (line beta1 k, color(red)) (line ul1 k, lpattern(shortdash) color(red)) (line ll1 k, lpattern(shortdash) color(red)) (line ul2 k, lpattern(dot) color(red)) (line ll2 k, lpattern(dot) color(red)), ///
                ytitle(Log ZHVI) ///
                xtitle(Year) xlabel(2001(1)2013, labels angle(forty_five) valuelabel) ///
                graphregion(color(white)) bgcolor(white) ///
                ylabel(,nogrid) ///
                yline(0, lpattern(solid) lcolor(gs3)) ///
                legend(off)
graph export "../results/figureA6b.png", replace
restore


*Transaction value
ivreghdfe lntran_price lnpopulation d_lntran_price_9600 bachelorshare d_lnincome_9801 sa_nbr_bath age sa_sq sa_nbr_bed (lnchtc lnchtc_2* = chi_iv_ct chi_iv_ct_2*) if chinese100cash!= 0, absorb(timecounty) cluster(zipcode) 

forvalue i = 2001/2006{
local beta_`i' = _b[lnchtc_`i']
local se_`i' = _se[lnchtc_`i']
}

forvalue i = 2008/2013{
local beta_`i' = _b[lnchtc_`i']
local se_`i' = _se[lnchtc_`i']
}

capture drop beta1 ul1 ll1 k ul2 ll2
gen beta1 = 0 in 7
gen ul1 = 0 in 7
gen ll1 = 0 in 7
gen ul2 = 0 in 7
gen ll2 = 0 in 7

forvalue j = 1/6{
local i = `j' + 2000
display `i' `j'
replace beta1 = `beta_`i'' in `j'
replace ul1 = `beta_`i'' + 1.96*`se_`i'' in `j'
replace ll1 = `beta_`i'' - 1.96*`se_`i'' in `j'
replace ul2 = `beta_`i'' + 1.645*`se_`i'' in `j'
replace ll2 = `beta_`i'' - 1.645*`se_`i'' in `j'
}	
 
forvalue j = 8/13{
local i = `j' + 2000
display `i' `j'
replace beta1 = `beta_`i'' in `j'
replace ul1 = `beta_`i'' + 1.96*`se_`i'' in `j'
replace ll1 = `beta_`i'' - 1.96*`se_`i'' in `j'
replace ul2 = `beta_`i'' + 1.645*`se_`i'' in `j'
replace ll2 = `beta_`i'' - 1.645*`se_`i'' in `j'
}	

gen k = 2001 in 1
replace k = k[_n-1] + 1 if k == .

preserve
keep in 1/13
twoway (line beta1 k, color(red)) (line ul1 k, lpattern(shortdash) color(red)) (line ll1 k, lpattern(shortdash) color(red)) (line ul2 k, lpattern(dot) color(red)) (line ll2 k, lpattern(dot) color(red)), ///
                ytitle(Log Transaction Value) ///
                xtitle(Year) xlabel(2001(1)2013, labels angle(forty_five) valuelabel) ///
                graphregion(color(white)) bgcolor(white) ///
                ylabel(,nogrid) ///
                yline(0, lpattern(solid) lcolor(gs3)) ///
                legend(off)
graph export "../results/figureA6d.png", replace
restore
